package com.jxc.action.spgl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VBrand;
import com.jxc.model.vo.VRepertory;
import com.jxc.model.vo.VUser;
import com.jxc.service.BrandServiceI;
import com.jxc.service.RepertoryServiceI;
import com.jxc.service.UserServiceI;
import com.jxc.util.ExceptionUtil;
import com.jxc.util.ResourceUtil;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/spgl")
@Action(value = "brandAction", results = { @Result(name = "brand", location = "/brand/brand.jsp"),
		@Result(name = "brandAdd", location = "/brand/brandAdd.jsp"), @Result(name = "brandEdit", location = "/brand/brandEdit.jsp") })
public class BrandAction extends BaseAction implements ModelDriven<VBrand> {
	private final static Logger logger = Logger.getLogger(BrandAction.class);

	private VBrand model = new VBrand();

	@Autowired
	private BrandServiceI brandService;
	@Autowired
	private UserServiceI userService;
	@Autowired
	private RepertoryServiceI repertoryService;

	public String brand() {
		getSubAuths("SPGL_PPGL");
		return "brand";
	}

	public String brandAdd() {
		return "brandAdd";
	}

	public String brandEdit() {
		return "brandEdit";
	}


	public void noAuth_combobox() {
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
			VUser vuser = userService.get(sessionInfo.getUserId());
			if (vuser.getReid() != null) {
				model.setUid(vuser.getUpid());
				List<VBrand> vbrands = brandService.combobox(model);
				this.writeJson(vbrands);
			} else {
//				List<VBrand> vbrands = new ArrayList<VBrand>();
//				VRepertory vrepertory = new VRepertory();
//				vrepertory.setUid(sessionInfo.getUserId());
//				List<VRepertory> vrepertories = repertoryService.list(vrepertory);
//				if (vrepertories != null && vrepertories.size() > 0) {
//					model.setRid(vrepertories.get(0).getId());
//					vbrands.addAll(brandService.combobox(model));
//				}
				model.setUid(sessionInfo.getUserId());
				List<VBrand> vbrands = brandService.combobox(model);
				this.writeJson(vbrands);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void findDiscountNum(){
		try {
			if(model.getId()!=null){
				Json j = new Json();
				j.setObj(brandService.findDiscountNum(model.getId()));
				this.writeJson(j);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void datagrid() {
		// 查询当前用户所属仓库，将仓库添加到查询条件中
		// 如当前用户没有所属仓库，则查询经销商为当前用户的仓库，将所有仓库中的品牌查询出来
		// 如当前用户没有所属仓库，也没有哪个仓库的经销商为当前用户，则返回为空！
		
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		if (model.getRid() != null) {
			this.writeJson(brandService.datagrid(model));
		} else {
			if (vuser.getReid() != null) {
				model.setRid(vuser.getReid());
				this.writeJson(brandService.datagrid(model));
			} else {
				model.setUid(sessionInfo.getUserId());
				VRepertory vrepertory = new VRepertory();
				vrepertory.setUid(sessionInfo.getUserId());
				List<VRepertory> vrepertories = repertoryService.list(vrepertory);
				if (vrepertories != null && vrepertories.size() > 0) {
					this.writeJson(brandService.datagrid(model));
				} else {
					DataGrid dg = new DataGrid();
					dg.setTotal(0l);
					dg.setRows(new ArrayList<VBrand>());
					this.writeJson(dg);
				}
			}
		}

	}

	/**
	 * 添加品牌
	 * 
	 */
	public void add() {
		Json j = new Json();
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
			model.setUid(sessionInfo.getUserId()); 
			brandService.save(model);
			j.setMsg("添加成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg("添加失败，品牌已存在！");
			logger.info(ExceptionUtil.getExceptionMessage(e));
		}
		super.writeJson(j);
	}

	/**
	 * 修改品牌
	 * 
	 */
	public void edit() {
		Json j = new Json();
		try {
			System.out.println(model.getRid1());
			brandService.update(model);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("编辑失败，品牌名称已存在！");
		}
		super.writeJson(j);
	}

	/**
	 * 删除品牌
	 * 
	 */
	public void delete() {
		try {
			Json j = new Json();
			brandService.delete(model.getIds());
			j.setSuccess(true);
			//j.setMsg("删除成功!");
			super.writeJson(j);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public VBrand getModel() {
		return model;
	}

}
